package nl.ajonker.railroad.pl;

import nl.ajonker.railroad.dl.Track;
import nl.ajonker.railroad.ui.drawcommand.DrawDoubleSwitchCommand;
import nl.ajonker.railroad.ui.drawcommand.DrawSwitchCommand;

import java.awt.*;
import java.util.LinkedList;
import java.util.List;

import static nl.ajonker.railroad.pl.RailView.SwitchType.DOUBLE;
import static nl.ajonker.railroad.pl.RailView.SwitchType.LEFT;
import static nl.ajonker.railroad.pl.RailView.SwitchType.RIGHT;

public class TrackView {
    private final LinkedList<RailView> rails = new LinkedList<RailView>();
    private final Track track;

    public TrackView(Track track, Point start, int dimension) {
        this.track = track;
        DrawSwitchCommand drawSwitchCommand = new DrawSwitchCommand();

        RailView switch1 = new RailView(track.getRail("1"), drawSwitchCommand, new Point(start.x + (int) Math.round(dimension * 0.85), start.y + (int) Math.round(dimension * 0.3)), RIGHT, -32);
        rails.add(switch1);

        RailView switch2 = new RailView(track.getRail("2"), drawSwitchCommand, new Point(start.x + dimension * 2, start.y), RIGHT);
        rails.add(switch2);

        RailView switch3 = new RailView(track.getRail("3"), drawSwitchCommand, new Point(start.x + dimension * 6, start.y), LEFT, 180);
        rails.add(switch3);

        RailView switch4 = new RailView(track.getRail("4"), drawSwitchCommand, new Point(start.x + dimension * 6, start.y), RIGHT);
        rails.add(switch4);

        RailView switch5 = new RailView(track.getRail("5"), drawSwitchCommand, new Point(start.x + dimension * 10, start.y), LEFT, 180);
        rails.add(switch5);

        RailView switch6 = new RailView(track.getRail("6"), drawSwitchCommand, new Point(start.x + (int) Math.round(dimension * 11.1), start.y + (int) Math.round(dimension * 0.35)), LEFT, -145);
        rails.add(switch6);

        RailView switch7 = new RailView(track.getRail("7"), drawSwitchCommand, new Point(start.x + (int) Math.round(dimension * 0.85), start.y + (int) Math.round(dimension * 3.7)), LEFT, 31);
        rails.add(switch7);

        RailView switch8 = new RailView(track.getRail("8"), drawSwitchCommand, new Point(start.x + dimension * 2, start.y + dimension * 4), LEFT);
        rails.add(switch8);

        RailView switch9 = new RailView(track.getRail("9"), drawSwitchCommand, new Point(start.x + dimension * 6, start.y + dimension * 4), RIGHT, 180);
        rails.add(switch9);

        RailView switch10 = new RailView(track.getRail("10"), drawSwitchCommand, new Point(start.x + dimension * 6, start.y + dimension * 4), LEFT);
        rails.add(switch10);

        RailView switch11 = new RailView(track.getRail("11"), drawSwitchCommand, new Point(start.x + dimension * 10, start.y + dimension * 4), RIGHT, 180);
        rails.add(switch11);

        RailView switch12 = new RailView(track.getRail("12"), drawSwitchCommand, new Point(start.x + (int) Math.round(dimension * 11.1), start.y + (int) Math.round(dimension * 3.7)), RIGHT, 145);
        rails.add(switch12);

        DrawDoubleSwitchCommand drawDoubleSwitchCommand = new DrawDoubleSwitchCommand();
        RailView switch13 = new RailView(track.getRail("13"), drawDoubleSwitchCommand, new Point(start.x + dimension * 4, start.y + dimension * 2), DOUBLE);
        rails.add(switch13);

        RailView switch14 = new RailView(track.getRail("14"), drawDoubleSwitchCommand, new Point(start.x + dimension * 8, start.y + dimension * 2), DOUBLE);
        rails.add(switch14);
    }

    public List<RailView> getRailViews() {
        return rails;
    }

    public void switchRailPosition(String name) {
        track.switchRailPosition(name);
    }
}
